package com.xyh.controller;

import com.google.gson.Gson;
import com.xyh.bean.Area;
import com.xyh.bean.ManagerClass;
import com.xyh.service.IClassService;
import com.xyh.service.impl.ClassServiceImpl;
import com.xyh.util.PageUtil;
import com.xyh.vo.AreaVO;
import com.xyh.vo.ClassVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: XiongYuhao
 * @Date: 2020/10/26 0026-上午 11:28
 * @Description: MyForestManagerSystem
 * @version：1.0
 */
@WebServlet(name = "ClassServlet",urlPatterns = "/cs.do")
public class ClassServlet extends HttpServlet {
    IClassService ics = new ClassServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method.equalsIgnoreCase("class_detail_page")){
            //小班详情并分页
            classDetailByPage(request,response);
        }else if(method.equalsIgnoreCase("update_class")){
            //更新小班信息
            classUpdate(request,response);
        }else if(method.equalsIgnoreCase("add_class")){
            //添加小班
            classAdd(request,response);
        }
    }
    private void classAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String className = request.getParameter("className");
        String managerPeople = request.getParameter("managerPeople");
        String tel = request.getParameter("tel");
        if(className.equalsIgnoreCase("")||managerPeople.equalsIgnoreCase("")||tel.equalsIgnoreCase("")){
            return;
        }
        String snum = request.getParameter("number");
        int number = 0;
        if(snum.equalsIgnoreCase("")){
            return;
        }else{
            number = Integer.parseInt(snum);
        }
        String sid = request.getParameter("areaName");
        int areaId = 0;
        if(sid.equalsIgnoreCase("")){
            return;
        }else{
            areaId = Integer.parseInt(sid);
        }

        Area area = new Area();
        area.setAreaId(areaId);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String createTime = sdf.format(date);

        ManagerClass mc = new ManagerClass(className,managerPeople,tel,number,area,createTime);
        boolean flag = ics.addClass(mc);
        Gson gson = new Gson();
        String json = gson.toJson(flag);
        response.getWriter().write(json);
    }

    private void classUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int classId = Integer.parseInt(request.getParameter("classId"));
        String managerPeople = request.getParameter("managerPeople");
        String tel = request.getParameter("tel");

        ManagerClass mc = new ManagerClass();
        mc.setClassId(classId);
        mc.setManagerPeople(managerPeople);
        mc.setTel(tel);

        boolean flag = ics.updateClass(mc);
        Gson gson = new Gson();
        String json = gson.toJson(flag);
        response.getWriter().write(json);
    }

    private void classDetailByPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String pn = request.getParameter("pageNo");
        int pageNo = 1;
        if(pn == null){
            pageNo = 1;
        }else{
            pageNo = Integer.parseInt(pn);
        }
        String ps = request.getParameter("pageSize");
        int pageSize = Integer.parseInt(ps);

        String className = request.getParameter("className");
        String managerArea = request.getParameter("areaName");
        PageUtil<ClassVO> pageBean = ics.listByPage(pageNo,pageSize,className,managerArea);
        Gson gson = new Gson();
        String json = gson.toJson(pageBean);
        response.getWriter().write(json);
    }
}
